package com.glsc.ngateway.opmanage.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.sql.DataSource;

/**
 * @Author: zzp
 * @Date: 20200527
 * @Desc: 多DataSource配置
 */

@Configuration
public class DataSourceDefConfig {
    private static Logger logger = LoggerFactory.getLogger(DataSourceDefConfig.class);

    public static final String DS_MYSQL = "datasourceMysql";
    public static final String DS_ORACLE = "datasourceOracle";
    public static final String DS_ORACLE_YONG_YOU = "datasourceOracleYongYou";
    public static final String DS_ORACLE_XIN_YI_ZJ = "datasourceOracleXinYiZj";
    public static final String DS_MYSQL_GLSC_DATA = "datasourceMysqlglscdata";
    public static final String POSTGRE_6 = "datasourcePostgre6";

    @Primary
    @Bean(name = DS_MYSQL)
    @ConfigurationProperties("spring.datasource.mysql")
    public DataSource datasourceMysql() {
        DataSource dataSource = DataSourceBuilder.create().build();
        return dataSource;
    }

    @Bean(name = DS_MYSQL_GLSC_DATA)
    @ConfigurationProperties("spring.datasource.mysqlglscdata")
    public DataSource datasourceMysqlglscdata() {
        return DataSourceBuilder.create().build();
    }

//    @Bean(name = POSTGRE_6)
//    @ConfigurationProperties("spring.datasource.postgre6")
//    public DataSource datasourceMysqlPostgre6() {
//        return DataSourceBuilder.create().build();
//    }

    @Bean(name = DS_ORACLE)
    @ConfigurationProperties("spring.datasource.oracle")
    public DataSource datasourceOracle() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = DS_ORACLE_YONG_YOU)
    @ConfigurationProperties("spring.datasource.oracle-yongyou")
    public DataSource datasourceYongYou() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = DS_ORACLE_XIN_YI_ZJ)
    @ConfigurationProperties("spring.datasource.oracle-xinyizj")
    public DataSource datasourceXinYiZj() {
        return DataSourceBuilder.create().build();
    }

//    @Bean(name = "mysqlglscdataJdbcTemplate")
//    JdbcTemplate mysqlglscdataJdbcTemplate() {
//        return new JdbcTemplate(datasourceMysqlglscdata());
//    }

    //三个jdbcTemplate
    @Bean(name = "mysqlglscdataJdbcTemplate")
    JdbcTemplate mysqlglscdataJdbcTemplate() {
        return new JdbcTemplate(datasourceMysqlglscdata());
    }

//    @Bean(name = "jdbcTemplatePostgre")
//    JdbcTemplate jdbcTemplatePostgre() {
//        return new JdbcTemplate(datasourcePostgre());
//    }

//    @Bean(name = "jdbcTemplatePostgre6")
//    JdbcTemplate jdbcTemplatePostgre6() {
//        return new JdbcTemplate(datasourceMysqlPostgre6());
//    }


    //三个NamedParameterJdbcTemplate
    @Bean(name = "namedParameterJdbcTemplateMysql")
    NamedParameterJdbcTemplate namedParameterJdbcTemplateMysql() {
        return new NamedParameterJdbcTemplate(datasourceMysqlglscdata());
    }


//    @Bean(name = "namedParameterJdbcTemplatePostgre")
//    NamedParameterJdbcTemplate namedParameterJdbcTemplatePostgre() {
//        return new NamedParameterJdbcTemplate(datasourcePostgre());
//    }

//    @Bean(name = "namedParameterJdbcTemplatePostgre6")
//    NamedParameterJdbcTemplate namedParameterJdbcTemplatePostgre6() {
//        return new NamedParameterJdbcTemplate(datasourceMysqlPostgre6());
//    }

}